#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

use gri_secmgr qw(get_cookie_value got_cookie);
use gri_database;
use Data::Dumper;
use MIME::Base64;

sub mm_form_docrepos_upload_file_populate {
my $file_name=shift;
my $file_description=shift;
my $file_actual=shift;
my $file_section=shift;
my $lp=shift;

	if(! got_cookie(name => "gri_dbm_intranet")) {
		print STDERR "mm_send_email: User not logged in - no email sent!\n";
		return "Unable to send mail - user not logged in.";
	}
	my $v=get_cookie_value(name=>"gri_dbm_intranet");
	if($v =~ /^public\s+/) {
		print STDERR "mm_form_docrepos_upload_file: User logged in as public - upload not possible!\n";
		return "Unable to upload file - user logged in as 'public'";
	}
	$v=~ s/\s+.*//; my $uploader=$v;
	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	$file_name="" if $file_name eq "?file_name - not defined?";
	$file_description="" if $file_description eq "?file_description - not defined?";
	$file_actual="" if $file_actual eq "?file_actual - not defined?";
	$file_section="" if $file_section eq "?file_section - not defined?";

	$file_name=hex_to_string($file_name);
	$file_description=hex_to_string($file_description);
	$file_actual=hex_to_string($file_actual);
	$file_section=decode_base64($file_section) if length($file_section);

	#################################################################################
	# We allow the title and description to be defaulted to "default" - useful	#
	# for upload/only options...							#
	#################################################################################
	my $default_title=0;
	my $default_description=0;
	if(length($file_section)) {
		my ($a,$b)=$DB->get_section(section=>$file_section);
		my $attr="";
		if($a==1) {
			$attr=$b->{attributes};
		}
		$attr =~ s/\s+//g;
		my @a2=split(/,/,$attr);
		if(grep {$_ eq "default_title"} @a2) {
			$default_title=1;
		}
		if(grep {$_ eq "default_description"} @a2) {
			$default_description=1;
		}
	}

my $s="
    <tr class=gri_headerrow>
      <td class=gri_cell colspan=3>New Document Repository Upload Details</td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>";
	if($default_title==0) {
$s.="
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Name:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <INPUT TYPE=\"text\" VALUE=\"$file_name\" NAME=\"name\" SIZE=\"60\" MAXLENGTH=\"40\">
      </td>
    </tr>
    <tr class=gri_row>
      <td colspan=3 align=left>&nbsp;</td>
    </tr>";
}
	if($default_description==0) {
$s.="
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Description:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <TEXTAREA NAME=\"description\" COLS=60 ROWS=5>$file_description</TEXTAREA>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;</td>
    </tr>";
}
$s.="
    <tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>File to Upload:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>
        <INPUT TYPE=\"file\" NAME=\"actual\" SIZE=40>
      </td>
    </tr>
    <tr class=gri_row>
      <td class=gri_cell colspan=3>&nbsp;<INPUT TYPE=\"hidden\" NAME=\"lp\" VALUE=\"$lp\"></td>
    </tr>\n";

    # We must get a list of sections the current user has access to...
    my ($x,$y)=$DB->get_user_sections(user=>$v,type=>"publisher");
    if($x!=1) {
	return "Unable to upload file - invalid results from get_user_sections call";
    }

    $s.="<tr class=gri_row>
      <td class=gri_cell><font class=stdtextlarge>Published in:</font></td>
      <td class=gri_cell>&nbsp;&nbsp;&nbsp;</td>
      <td class=gri_cell>\n";
    my %sec_maps=();
    for my $cg (@$y) {
	my ($c,$d)=$DB->get_section(section=>$cg);
	if($c==1) {
		$sec_maps{$cg}=$d->{title};
	}
    }
    my @sec_list=sort {$sec_maps{$a} cmp $sec_maps{$b}} (keys %sec_maps);
    
    for my $cg (@sec_list) {
	if((exists($::PARAM{"section_$cg"}) && $::PARAM{"section_$cg"}==1) || 
		(length($file_section) && $file_section eq $cg)) {
		$s.="<input type=checkbox name=\"section_$cg\" value=1 checked=yes>$sec_maps{$cg} [$cg]<br>";
	 } else {
		$s.="<input type=checkbox name=\"section_$cg\" value=1>$sec_maps{$cg} [$cg]<br>";
	}
    }

	if($default_title) {
		$s.="<input type=hidden name=\"name\" VALUE=\"default\">";
	}
	if($default_description) {
		$s.="<input type=hidden name=\"description\" VALUE=\"Default description\">";
	}
        $s.="<INPUT type=hidden name=uploader value=\"$uploader\"/>";
    $s.="\n</td></tr>\n";
    return $s;
}

1;

